import { defineStore } from 'pinia';

// 定义用户信息类型
interface UserInfo {
  username: string;
  [key: string]: any;
}

// 创建并导出auth store
export const useAuthStore = defineStore('auth', {
  state: () => ({
    userInfo: null as UserInfo | null,
    token: localStorage.getItem('admin-token') || null
  }),

  getters: {
    isLoggedIn: (state) => !!state.token
  },

  actions: {
    // 设置用户信息
    setUserInfo(userInfo: UserInfo) {
      this.userInfo = userInfo;
    },

    // 保存token
    saveToken(token: string) {
      this.token = token;
      localStorage.setItem('admin-token', token);
    },

    // 获取用户信息
    getUserInfo() {
      return this.userInfo;
    },

    // 退出登录
    logout() {
      this.userInfo = null;
      this.token = null;
      localStorage.removeItem('admin-token');
    }
  }
});